const BaseService = require("./baseService");

class GoodsInfoService extends BaseService {
    constructor(){
        super(...arguments);
        this.tableName="goodsinfo";
    }

    async addGoodsInfo({goodsname, goodsprice, goodsoffset, goodsimg, goodscount, goodsstatus, goodsproperty,categoryid}){
        let result =await this.app.mysql.insert(this.tableName,{
            goodsname, goodsprice, goodsoffset, goodsimg, goodscount, goodsstatus, goodsproperty,categoryid
        });
        return result.affectedRows>0?true:false;
    }
    async getListByPage({categoryid,goodsname,minprice,maxprice,pageIndex}={}){
        pageIndex=pageIndex||1;
        let strWhere="";
        if(goodsname){
            strWhere+=" and goodsname like '%"+goodsname+"%'";
        }
        if(minprice){
            strWhere+= " and goodsprice>='"+minprice+"'";
        }
        if(maxprice){
            strWhere+=" and goodsprice <='"+maxprice+"'";
        }
        if(categoryid){
            strWhere+=" and categoryid='"+categoryid+"'";
        }
        let strSql=`select * from goodsinfoview where isDel=false ${strWhere} limit ?,? `;
        let countSql=this.createCountSql(strWhere);
        let result = await this.app.mysql.query(strSql+";"+countSql,[(pageIndex-1)*this.pageSize,this.pageSize]);
        return result;
    }

    /**
     * 
     * @param {Array} cksValue 删除的数组
     */
    async deleteGoodsInfoList(cksValue) {
        let result = await this.app.mysql.update(this.tableName, {
            isDel: true
        }, {
            where: {
                goodsid: cksValue
            }
        });
        return result.affectedRows > 0 ? true : false;
    }

    async findByGoodsId(goodsid){
        let result = await this.app.mysql.get(this.tableName,{goodsid,isDel:false});
        return result;
    }

    async editGoodsInfo({goodsid,goodsname,goodsprice,goodsoffset,goodscount,goodsstatus,goodsproperty,categoryid}){
        let result = await this.app.mysql.update(this.tableName,{
            goodsname,goodsprice,goodsoffset,goodscount,goodsstatus,goodsproperty,categoryid
        },{
            where:{
                goodsid
            }
        });
        return result.affectedRows>0?true:false;
    }
    
}

module.exports=GoodsInfoService;